from rest_framework import serializers
from patients.models import PatientModel
from django.utils.timezone import now


class PatientModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = PatientModel
        fields = '__all__'


class PatientListModelSerializer(serializers.ModelSerializer):
    age = serializers.SerializerMethodField()
    sex = serializers.SerializerMethodField()

    class Meta:
        model = PatientModel
        fields = ['id', 'name', 'sex', 'age', 'birthday']

    def get_age(self, obj):
        today = now().date()
        age = today.year - obj.birthday.year - ((today.month, today.day) < (obj.birthday.month, obj.birthday.day))
        return age

    def get_sex(self, obj):
        if obj.sex:
            return "女"
        else:
            return "男"
